package menu

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"wms/model"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddLogic) Add(req *types.MenuAddRequest) (resp *types.BaseResponse, err error) {
	resp = new(types.BaseResponse)
	//1.父路由id是否存在
	if req.ParentId > 0 {
		var parent *model.Menu
		parent, err = l.svcCtx.MenuModel.FindOne(l.ctx, req.ParentId)
		switch err {
		case nil:
			if parent.Type != 1 {
				resp.Code = http.StatusBadRequest
				resp.Msg = "只能在菜单上执行添加操作"
				return resp, nil
			}
		case model.ErrNotFound:
			resp.Code = http.StatusBadRequest
			resp.Msg = "父路由不存在"
			return resp, nil
		default:
			fmt.Printf("[Error]查询父路由[%d]:%s\n", req.ParentId, err.Error())
			resp.Code = http.StatusBadRequest
			resp.Msg = "服务内部错误"
			return resp, nil
		}
	}

	//2.查询路由是否重复：path、name
	if strings.TrimSpace(req.Path) != "" {
		var ms = make([]model.Menu, 0)
		ms, err = l.svcCtx.MenuModel.FindByPage(l.ctx, fmt.Sprintf(" where path='%s' ", req.Path))
		switch err {
		case nil:
			if len(ms) > 0 {
				fmt.Printf("[Error]路由路径[%s]重复\n", req.Path)
				resp.Code = http.StatusBadRequest
				resp.Msg = "路由路径重复"
				return resp, nil
			}

		default:
			fmt.Printf("[Error]查询路由路径[%s]:%s\n", req.Path, err.Error())
			resp.Code = http.StatusBadRequest
			resp.Msg = "服务内部错误"
			return resp, nil
		}
	}

	//if strings.TrimSpace(req.Name) != "" {
	//	var ms = make([]model.Menu, 0)
	//	ms, err = l.svcCtx.MenuModel.FindByPage(l.ctx, fmt.Sprintf(" where name='%s' ", req.Name))
	//	switch err {
	//	case nil:
	//		if len(ms) > 0 {
	//			fmt.Printf("[Error]路由名称[%s]重复\n", req.Name)
	//			resp.Code = http.StatusBadRequest
	//			resp.Msg = "路由名称重复"
	//			return resp, nil
	//		}
	//	default:
	//		fmt.Printf("[Error]查询路由名称[%s]:%s\n", req.Name, err.Error())
	//		resp.Code = http.StatusBadRequest
	//		resp.Msg = "服务内部错误"
	//		return resp, nil
	//	}
	//}

	//3.路由入库
	meta, err := json.Marshal(&types.Meta{
		Icon:       req.Icon,
		Transition: req.Transition,
		Hidden:     req.Hidden,
		Fixed:      req.Fixed,
		Perms:      req.Perms,
	})
	if err != nil {
		fmt.Printf("[Error]路由[%s]元信息编码:%s\n", req.Path, err.Error())
		resp.Code = http.StatusBadRequest
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	var menu = model.Menu{
		Type:      req.Type,
		SortId:    req.SortId,
		ParentId:  req.ParentId,
		Path:      strings.TrimSpace(req.Path),
		Name:      req.Name,
		Component: strings.TrimSpace(req.Component),
		Meta:      string(meta),
		Remark:    req.Remark,
	}

	_, err = l.svcCtx.MenuModel.Insert(l.ctx, &menu)

	if err != nil {
		fmt.Printf("[Error]路由[%s]入库:%s\n", req.Path, err.Error())
		resp.Code = http.StatusBadRequest
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	return resp, nil
}
